package com.flash.service.impl;

import com.flash.context.BaseContext;
import com.flash.dto.ShoppingCartDTO;
import com.flash.entity.Dish;
import com.flash.entity.Setmeal;
import com.flash.entity.ShoppingCart;
import com.flash.mapper.DishMapper;
import com.flash.mapper.SetmealMapper;
import com.flash.mapper.ShoppingCartMapper;
import com.flash.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void insert(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断购物车中的商品是否已经存在
        //如果是套餐id，那么根据套餐id查询，如果是菜品id，要查询本口味的本菜品是否已经存在;
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && !list.isEmpty()){
            //如果已经存在那么将数量加一
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.addIfExtist(cart);
            return;
        }
        //如果不存在那么将菜品插入进购物车
        //判断本次添加的是菜品还是套餐
        if (shoppingCart.getDishId() != null){
            //添加的是菜品
            Dish dish = dishMapper.selectById(shoppingCart.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }else {
            //本次添加进来的是套餐
            Setmeal setmeal = setmealMapper.getSetMealById(shoppingCart.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 查询用户购物车中的数据并且返回给用户
     * @return
     */
    @Override
    public List<ShoppingCart> listAll() {
        Long userId = BaseContext.getCurrentId();
       List<ShoppingCart> cartList =  shoppingCartMapper.listAll(userId);
       return cartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanChart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.cleanChart(userId);
    }

    /**
     * 删除购物车中的某一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //获取到当前商品的信息
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart Cart = list.get(0);
        //当前商品数量-1>0的时候执行减1，当前商品数量-1=0的时候执行delete这条记录
        if (Cart.getNumber() - 1 > 0){
            Cart.setNumber(Cart.getNumber() - 1);
            shoppingCartMapper.sub(Cart);
        }else {
            shoppingCartMapper.delete(Cart);
        }

    }
}

